/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.core.util.uuid.UUIDGenerator;
import cn.qidisoft.edu.hzjt.dao.BjxxClassMaterialProjectMapper;
import cn.qidisoft.edu.hzjt.dao.BjxxClassMaterialRangeMapper;
import cn.qidisoft.edu.hzjt.dao.OfDoingsMapper;
import cn.qidisoft.edu.hzjt.model.BjxxClassMaterialProject;
import cn.qidisoft.edu.hzjt.model.BjxxClassMaterialRange;
import cn.qidisoft.edu.hzjt.model.OfDoings;
import cn.qidisoft.edu.hzjt.service.IBjxxClassMaterialProjectService;
import cn.qidisoft.edu.hzjt.utils.LogType;

/**
 * 
 * 
 * @author Administrator
 * @version $Id: BjxxClassMaterialProjectServiceImpl.java, v 0.1 2018年10月29日 下午1:14:36 hhl Exp $
 */
@Service("bjxxClassMaterialProjectService")
public class BjxxClassMaterialProjectServiceImpl implements IBjxxClassMaterialProjectService {

    protected static final LoggerAdapter   LOGGER = LoggerAdapterFactory
        .getLogger(LogType.HZJT_LOGGER.val);

    @Autowired
    private BjxxClassMaterialProjectMapper bjxxClassMaterialProjectMapper;
    @Autowired
    private BjxxClassMaterialRangeMapper   bjxxClassMaterialRangeMapper;

    @Autowired
    private OfDoingsMapper                 ofDoingsMapper;

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryList(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public List<BjxxClassMaterialProject> queryList(BjxxClassMaterialProject data) {
        try {
            return bjxxClassMaterialProjectMapper.queryList(data);

        } catch (Exception e) {
            LOGGER.error("queryList [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryByPK(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public BjxxClassMaterialProject queryByPK(BjxxClassMaterialProject data) {
        try {
            return bjxxClassMaterialProjectMapper.queryByPK(data);
        } catch (Exception e) {
            LOGGER.error("queryByPK [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#add(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public String add(BjxxClassMaterialProject data, Integer uid) {
        String gid = UUIDGenerator.genReqID();
        try {
            data.setGid(gid);
            bjxxClassMaterialProjectMapper.add(data);
            /* if (data.getIssued() == true) {
                OfDoings ofDoings = new OfDoings();
                ofDoings.setMaster(uid);
                ofDoings.setSubject("您有新的班级材料项目");
                ofDoings.setUrgent((byte) 2);
                ofDoings.setImportance((byte) 2);
                ofDoings.setDatefrom(data.getIssuetime());
                ofDoings.setDeadline(null);
                ofDoings.setContent("您的新班级材料项目:" + data.getXmmc() + ",请及时提交班级材料");
                ofDoings.setUrl("班级材料项目:" + data.getXmmc());
                ofDoings.setIssystem(false);
                ofDoings.setConfirmed(false);
                ofDoings.setCreatetime(new Date());
                ofDoingsMapper.add(ofDoings);
            }*/
        } catch (Exception e) {
            LOGGER.error("add [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
        return gid;
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#update(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public void update(BjxxClassMaterialProject data, Integer uid, String basePath) {
        try {
            bjxxClassMaterialProjectMapper.update(data);
            if (data.getIssued() == true) {
                BjxxClassMaterialRange bjxxClassMaterialRange = new BjxxClassMaterialRange();
                bjxxClassMaterialRange.setParentid(data.getXmid());
                List<BjxxClassMaterialRange> list = bjxxClassMaterialRangeMapper
                    .queryList(bjxxClassMaterialRange);

                if (list != null && !list.isEmpty()) {

                    for (BjxxClassMaterialRange r : list) {
                        OfDoings ofDoings = new OfDoings();
                        ofDoings.setMaster(r.getUid());
                        ofDoings.setSubject("您有新的班级材料项目");
                        ofDoings.setUrgent((byte) 2);
                        ofDoings.setImportance((byte) 2);
                        ofDoings.setDatefrom(data.getIssuetime());
                        ofDoings.setDeadline(null);
                        ofDoings
                            .setContent(basePath + "您的新班级材料项目:" + data.getXmmc() + ",请及时提交班级材料");
                        ofDoings.setUrl(
                            basePath + "/bjxxClassMaterialFile/details?type=t&userid=" + r.getUid()
                                        + "&id=" + r.getParentid() + "&rangeId=" + r.getRangeid());
                        ofDoings.setIssystem(false);
                        ofDoings.setConfirmed(false);
                        ofDoings.setCreatetime(new Date());
                        ofDoingsMapper.add(ofDoings);
                    }
                }
            }

        } catch (Exception e) {
            LOGGER.error("update [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#delete(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public void delete(BjxxClassMaterialProject data) {
        try {
            bjxxClassMaterialProjectMapper.delete(data);
        } catch (Exception e) {
            LOGGER.error("delete [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryPageList(cn.qidisoft.microClass.resource.model.basis.Catalog, cn.qidisoft.core.common.pagination.Page)
     */
    @Override
    public PaginationBean<BjxxClassMaterialProject> queryPageList(BjxxClassMaterialProject data,
                                                                  Page page) {
        try {

            int c = bjxxClassMaterialProjectMapper.queryPageListcount(data);
            int totalRecords = 0;
            totalRecords = (int) c;
            PaginationBean<BjxxClassMaterialProject> pageHolder = new PaginationBean<BjxxClassMaterialProject>(
                page, totalRecords);
            List<BjxxClassMaterialProject> resultList = bjxxClassMaterialProjectMapper
                .queryPageList(data,
                    new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                        pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#validation(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public int validation(BjxxClassMaterialProject data) {
        try {
            return bjxxClassMaterialProjectMapper.validation(data);
        } catch (Exception e) {
            e.printStackTrace();
            return -99;
        }
    }

    /** 
     * @see cn.qidisoft.edu.hzjt.service.IBjxxClassMaterialProjectService#queryPageList_bzr(cn.qidisoft.edu.hzjt.model.BjxxClassMaterialProject, cn.qidisoft.core.common.pagination.Page)
     */
    @Override
    public PaginationBean<BjxxClassMaterialProject> queryPageList_bzr(BjxxClassMaterialProject data,
                                                                      Page page) {
        try {

            int c = bjxxClassMaterialProjectMapper.queryPageListcount_bzr(data);
            int totalRecords = 0;
            totalRecords = (int) c;
            PaginationBean<BjxxClassMaterialProject> pageHolder = new PaginationBean<BjxxClassMaterialProject>(
                page, totalRecords);
            List<BjxxClassMaterialProject> resultList = bjxxClassMaterialProjectMapper
                .queryPageList_bzr(data,
                    new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                        pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            LOGGER.error("queryPageList [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.edu.hzjt.service.IBjxxClassMaterialProjectService#queryByPK_bzr(cn.qidisoft.edu.hzjt.model.BjxxClassMaterialProject)
     */
    @Override
    public BjxxClassMaterialProject queryByPK_bzr(BjxxClassMaterialProject data) {
        try {
            return bjxxClassMaterialProjectMapper.queryByPK_bzr(data);
        } catch (Exception e) {
            LOGGER.error("queryByPK [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.edu.hzjt.service.IBjxxClassMaterialProjectService#export(cn.qidisoft.edu.hzjt.model.BjxxClassMaterialProject)
     */
    @Override
    public List<BjxxClassMaterialProject> export(BjxxClassMaterialProject data) {
        try {
            return bjxxClassMaterialProjectMapper.export(data);
        } catch (Exception e) {
            LOGGER.error("export [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.edu.hzjt.service.IBjxxClassMaterialProjectService#export_bzr(cn.qidisoft.edu.hzjt.model.BjxxClassMaterialProject)
     */
    @Override
    public List<BjxxClassMaterialProject> export_bzr(BjxxClassMaterialProject data) {
        try {
            return bjxxClassMaterialProjectMapper.export_bzr(data);
        } catch (Exception e) {
            LOGGER.error("export_bzr [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

}
